import {createRouter, createWebHistory} from 'vue-router'
import main from './model/main'
import {getToken} from '@/utils/auth'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        ...main
    ]
})

router.beforeEach((to, from, next) => {
    document.title = to.meta.title
    const token = getToken()
    if (token) {
        if (to.path === '/login') {
            next('/')
        } else {
            next()
        }
    } else {
        if (to.path === '/login') {
            next()
        } else {
            next('/login')
        }
    }
})


export default router
